
//全局时间监听模块

Global.EventEngine = {
    events: {},
    addListener(event, callFun, owner) {
        this.events[event] = this.events[event] || [];
        this.events[event].push({
            owner: owner || this,
            callFun: callFun
        })
    },

    removeListener(event, callFun, owner) {
        owner = owner || this;
        let fns = this.events[event];

        if (!fns)
            return;

        this.events[event] = fns.filter(function (fn) {
            return fn.owner != owner || fn.callFun != callFun
        })
    },

    removeOwner(owner) {
        for (var msg in this.events) {
            var obs = this.events[msg];
            if (obs) {
                this.events[msg] = obs.filter(function (fn) {
                    if (fn.owner != owner) {
                        return true;
                    } else {
                        return false;
                    }
                })
            }
        }
    },

    dispatch(event, params) {
        console.info('<ZJY>--->>EventDispatch:' + event);
        var fns = this.events[event];
        if (!fns)
            return;

        var fn;
        for (var i = 0; i < fns.length; i++) {
            fn = fns[i];
            fn.callFun.call(fn.owner, params)
        }
    }

}